package com.sansui.blog.utils;

import com.sansui.blog.pojo.sys.SysEmail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import java.util.Properties;

/**
 * @name: SendEmail
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-11-26 08:59
 **/

//系统邮件通知
public class SendEmail {

    //host = mailsmtp服务地址
    //addressee = 收件人
    //emailUser = 发信人
    //title = 标题
    //Content = 内容
    //froma = 发件人
    //authorization =IMAP/SMTP服务授权码
    public static boolean aTest(String host,String port,String addressee,String emailUser,String title, String content, String froma, String authorization) {
        Boolean istrue = true;
        try {
            // 创建Properties 类用于记录邮箱的一些属性
            final Properties properties = new Properties();
            // 表示SMTP发送邮件，必须进行身份验证
            properties.put("mail.smtp.auth", "true");
            //此处填写SMTP服务器 "smtp.qq.com" smtp.exmail.qq.com
            properties.put("mail.smtp.host", host);
            //端口号
            properties.put("mail.smtp.port", port);
            // 此处填写，写信人的账号
            properties.put("mail.user", froma);
            // qq邮箱产生--授权码
            properties.put("mail.password", authorization);
            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = properties.getProperty("mail.user");
                    String password = properties.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }

            };
            // 使用环境属性和授权信息，创建邮件会话
            Session session = Session.getInstance(properties, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(session);
            // 设置发件人 properties.getProperty("mail.user")
            InternetAddress from = new InternetAddress(properties.getProperty("mail.user"),emailUser,"UTF-8");
            message.setFrom(from);
            //收件人
            InternetAddress to = new InternetAddress(addressee);
            message.setRecipient(RecipientType.TO, to);
            // 设置邮件标题
            message.setSubject(title);
            // 设置邮件的内容体
            message.setContent(content, "text/html;charset=UTF-8");
            //发送邮件
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            istrue = false;
        }
        return istrue;
    }

//    public static void main(String[] args) {
//
//        String emailContent = "<div>\n" +
//                "    <includetail>\n" +
//                "        <div align=\"center\">\n" +
//                "            <div class=\"open_email\" style=\"margin-left: 8px; margin-top: 8px; margin-bottom: 8px; margin-right: 8px;\">\n" +
//                "                <div>\n" +
//                "                    <br>\n" +
//                "                    <span class=\"genEmailContent\">\n" +
//                "\t\t\t\t\t\t<div id=\"cTMail-Wrap\"\n" +
//                "                             style=\"word-break: break-all;box-sizing:border-box;text-align:center;min-width:320px; max-width:660px; border:1px solid #f6f6f6; background-color:#f7f8fa; margin:auto; padding:20px 0 30px; font-family:'helvetica neue',PingFangSC-Light,arial,'hiragino sans gb','microsoft yahei ui','microsoft yahei',simsun,sans-serif\">\n" +
//                "\t\t\t\t\t\t\t<div class=\"main-content\" style=\"\">\n" +
//                "\t\t\t\t\t\t\t\t<table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse\">\n" +
//                "\t\t\t\t\t\t\t\t\t<tbody>\n" +
//                "\t\t\t\t\t\t\t\t\t\t<tr style=\"font-weight:300\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t<td style=\"width:3%;max-width:30px;\"></td>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t<td style=\"max-width:600px;\">\n" +
//                "                                                <h3>博客评论通知</h3>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t<p style=\"height:2px;background-color: #00a4ff;border: 0;font-size:0;padding:0;width:100%;margin-top:20px;\"></p>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t<div id=\"cTMail-inner\"\n" +
//                "                                                     style=\"background-color:#fff; padding:23px 0 20px;box-shadow: 0px 1px 1px 0px rgba(122, 55, 55, 0.2);text-align:left;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t<table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse;text-align:left;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t<tbody>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<tr style=\"font-weight:300\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<td style=\"width:3.2%;max-width:30px;\"></td>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<td style=\"max-width:480px;text-align:left;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<h1 id=\"cTMail-title\"\n" +
//                "                                                                        style=\"font-size: 20px; line-height: 36px; margin: 0px 0px 22px;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t#Re:{文章标题内容}\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</h1>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p id=\"cTMail-userName\"\n" +
//                "                                                                       style=\"font-size:14px;color:#333; line-height:24px; margin:0;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{评论内容}\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</p>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p class=\"cTMail-content\"\n" +
//                "                                                                       style=\"line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span style=\"color: rgb(51, 51, 51); font-size: 14px;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t评论者:{评论人}\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</span>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</p>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p class=\"cTMail-content\"\n" +
//                "                                                                       style=\"line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span style=\"color: rgb(51, 51, 51); font-size: 14px;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<br>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t URL:\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<br>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<a href=\"\" title=\"\"\n" +
//                "                                                                               style=\"color: rgb(0, 164, 255); text-decoration: none; word-break: break-all; overflow-wrap: normal; font-size: 14px;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{文章地址}\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</a>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</span>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</p>\n" +
//                "\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<dl style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<dd style=\"margin: 0px 0px 6px; padding: 0px; font-size: 12px; line-height: 22px;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p id=\"cTMail-sender\"\n" +
//                "                                                                               style=\"font-size: 14px; line-height: 26px; word-wrap: break-word; word-break: break-all; margin-top: 32px;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t此致\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<br>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<strong>博客运维团队</strong>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</p>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</dd>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</dl>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</td>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<td style=\"width:3.2%;max-width:30px;\"></td>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</tr>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t</tbody>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t</table>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t</div>\n" +
//                "\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t<div id=\"cTMail-copy\"\n" +
//                "                                                     style=\"text-align:center; font-size:12px; line-height:18px; color:#999\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t<table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t<tbody>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<tr style=\"font-weight:300\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<td style=\"width:3.2%;max-width:30px;\"></td>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<td style=\"max-width:540px;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p style=\"text-align:center; margin:20px auto 14px auto;font-size:12px;color:#999;\">\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t此为系统邮件，请勿回复。\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</p>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</td>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<td style=\"width:3.2%;max-width:30px;\"></td>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</tr>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t</tbody>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t\t</table>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t\t</div>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t</td>\n" +
//                "\t\t\t\t\t\t\t\t\t\t\t<td style=\"width:3%;max-width:30px;\"></td>\n" +
//                "\t\t\t\t\t\t\t\t\t\t</tr>\n" +
//                "\t\t\t\t\t\t\t\t\t</tbody>\n" +
//                "\t\t\t\t\t\t\t\t</table>\n" +
//                "\t\t\t\t\t\t\t</div>\n" +
//                "\t\t\t\t\t\t</div>\n" +
//                "\t\t\t\t\t</span>\n" +
//                "                </div>\n" +
//                "            </div>\n" +
//                "        </div>\n" +
//                "    </includetail>\n" +
//                "</div>";
//
//        aTest("smtp.qq.com","接收邮箱","三岁BLog系统通知","[博客评论通知]}Re:{文章标题内容}",emailContent,"发信邮箱","发信邮箱密码");
//        System.out.println("发送成功");
//    }

}
